package cz.proks.easyhelpdesk.gae.api.impl.ws;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cz.proks.easyhelpdesk.api.ThreadMemberApi;
import cz.proks.easyhelpdesk.api.config.Configuration;
import cz.proks.easyhelpdesk.api.exception.ApiException;
import cz.proks.easyhelpdesk.api.model.HelpdeskRole;
import cz.proks.easyhelpdesk.api.model.ThreadMember;

@Controller(Configuration.WS.THREAD_MEMBER_API)
@RequestMapping(Configuration.WS.THREAD_MEMBER_BASE_URL)
public class ThreadMemberApiImpl extends BaseApiImpl<ThreadMember> implements ThreadMemberApi {    
    private final Logger log = LoggerFactory.getLogger(getClass());
    
    @Autowired
    @Qualifier(Configuration.Core.WS.THREAD_MEMBER_API)
    private ThreadMemberApi coreApi;
    
    @Override
    public ThreadMemberApi getCoreApi() {
        return coreApi;
    }
    
    @Override
    @RequestMapping(value = "/getForHeldpeskMember/{helpdeskMemberId}/ForThread/{threadId}", method = RequestMethod.GET)
    @ResponseBody
    public ThreadMember getForHelpdeskMemberAndThread(@PathVariable Long helpdeskMemberId, @PathVariable Long threadId) throws ApiException {
        log.debug("getForHelpdeskMemberAndThread():: Start with helpdeskMemberId '{}', threadId '{}'", helpdeskMemberId, threadId);
        
        ThreadMember threadMember = getCoreApi().getForHelpdeskMemberAndThread(helpdeskMemberId, threadId);
        
        log.debug("getForHelpdeskMemberAndThread():: End with '{}'", threadMember);
        return threadMember;
    }

    @Override
    @RequestMapping(value = "/listForHelpdeskMember/{helpdeskMemberId}", method = RequestMethod.GET)
    @ResponseBody
    public List<ThreadMember> listForHelpdeskMember(@PathVariable Long helpdeskMemberId) throws ApiException {
        log.debug("listForHelpdeskMember():: Start with helpdeskMemberId '{}'", helpdeskMemberId);

        List<ThreadMember> threadMembers = getCoreApi().listForHelpdeskMember(helpdeskMemberId);

        log.debug("listForHelpdeskMember():: End with {}", (threadMembers == null ? "null list" : "list of " + threadMembers.size() + " elements"));
        return threadMembers;
    }    
    
    @Override
    @RequestMapping(value = "/{id}/setRead/{read}", method = RequestMethod.PUT)
    @ResponseBody
    public void setRead(Long id, boolean read) throws ApiException {
        log.debug("setRead():: Start with id '{}', read '{}'", id, read);
        
        getCoreApi().setRead(id, read);
        
        log.debug("setRead():: Exit");
    }
    
    @Override
    @RequestMapping(value = "/shareForHelpdesk/{helpdeskId}/ForThread/{threadId}/ForRole/{helpdeskRole}", method = RequestMethod.PUT)
    @ResponseBody
    public void shareForHelpdeskAndThreadAndRole(@PathVariable Long helpdeskId, @PathVariable Long threadId, @PathVariable HelpdeskRole helpdeskRole) throws ApiException {
        log.debug("shareForHelpdeskAndThreadAndRole():: Start with helpdeskId '{}', threadId '{}', helpdeskRole '{}'", new Object[] {helpdeskId, threadId, helpdeskRole});
        
        getCoreApi().shareForHelpdeskAndThreadAndRole(helpdeskId, threadId, helpdeskRole);
        
        log.debug("shareForHelpdeskAndThreadAndRole():: Exit");        
    }

    @Override
    @RequestMapping(value = "/shareForHelpdeskMember/{helpdeskMemberId}/ForRole/{helpdeskRole}", method = RequestMethod.PUT)
    @ResponseBody
    public void shareForHelpdeskMemberAndRole(@PathVariable Long helpdeskMemberId, @PathVariable HelpdeskRole helpdeskRole) throws ApiException {
        log.debug("shareForHelpdeskMemberAndRole():: Start with helpdeskMemberId '{}', helpdeskRole '{}'", helpdeskMemberId, helpdeskRole);
        
        getCoreApi().shareForHelpdeskMemberAndRole(helpdeskMemberId, helpdeskRole);
        
        log.debug("shareForHelpdeskMemberAndRole():: Exit");
    }
}
